package org.coc.betterchicago.util;

import javax.servlet.http.Cookie;

public class CookieManager {
	public static final String COOKIE_ID = "theid";
	public static final String COOKIE_URL = "authurl";

	public Cookie createCookie(Long value) {
		return createIdCookie(value.toString());
	}

	public static Cookie createAuthorizationUrlCookie(String value) {
		Cookie cookie = new Cookie(COOKIE_URL, value);
		cookie.setMaxAge(Integer.MAX_VALUE);
		return cookie;
	}

	public Cookie createIdCookie(String value) {
		Cookie cookie = new Cookie(COOKIE_ID, value);
		cookie.setMaxAge(Integer.MAX_VALUE);
		return cookie;
	}

	public Long parseIdCookie(Cookie[] cookies) {
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equalsIgnoreCase(COOKIE_ID)) {
					String cookieValue = cookie.getValue();
					return Long.valueOf(cookieValue);
				}
			}
		}
		return null;
	}
}
